package pinyinJava.utils;

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.Properties;

public class ProxyUtil {

    private static String proxyHost = null;
    private static String proxyPort = null;
    private static String userName = null;
    private static String password = null;

    public static void init() {
        if (proxyHost != null && proxyPort != null) {
            Properties prop = System.getProperties();
            prop.setProperty("http.proxyHost", proxyHost);
            prop.setProperty("http.proxyPort", proxyPort);
            prop.setProperty("https.proxyHost", proxyHost);
            prop.setProperty("https.proxyPort", proxyPort);
            if (userName != null && password != null) Authenticator.setDefault(new MyAuth());
        }
    }

    static class MyAuth extends Authenticator {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password.toCharArray());
        }
    }
}
